/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import org.openaecfm.model.kernel.ObjectDefinition;
import org.openaecfm.model.kernel.ObjectTypeEnum;
import org.openaecfm.model.kernel.RelAssigns;
import org.openaecfm.model.kernel.Root;
import org.openaecfm.service.kernel.IRelAssignsManager;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class RelAssignsIO implements
		IEntityInstanceUtilInput<RelAssigns>,
		IEntityInstanceUtilOutput<RelAssigns> {
	private IEntityInstanceUtilInput<Root> utilInput;
	private IEntityInstanceUtilOutput<Root> utilOutput;
	private IRelAssignsManager manager;

	public void setManager(IRelAssignsManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(RelAssigns pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatedObjects
		ListParameter ros = new ListParameter();
		for (ObjectDefinition od : pojo.getRelatedObjects()) {
			ros.add(new EntityInstanceNameParameter(appDataManager.getName(od)));
		}
		sei.addParameter(ros);

		// Set RelatedObjectsType
		if (pojo.getRelatedObjectsType() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EnumerationParameter(pojo
					.getRelatedObjectsType()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, RelAssigns pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatedObjectsType
		Parameter rotype = sei.getParameter(5);
		if (rotype.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) rotype;
			pojo.setRelatedObjectsType(ObjectTypeEnum.valueOf(e.toEnumString()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, RelAssigns pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatedObjects
		ListParameter ros = (ListParameter) sei.getParameter(4);
		for (Parameter p : ros) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			manager.addRelatedObject(pojo, (ObjectDefinition) appDataManager
					.getObject(n.toInstanceName()));
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}
}
